package com.edu.test;


import com.edu.bean.Emp;
import com.edu.bean.EmpVo;
import com.edu.bean.Grade;
import com.edu.bean.Student;
import com.edu.mapper.EmpMapper;
import com.edu.mapper.GradeMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyTest03 {

    private SqlSession sqlSession = null;

    @Before
    public void init() throws IOException {
        String path = "mybatis-config.xml";

        InputStream inputStream = Resources.getResourceAsStream(path);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        sqlSession = sqlSessionFactory.openSession();
    }

    @Test
    public void test(){
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = empMapper.selectEmpById(2);
        System.out.println(emp);
        System.out.println(emp.getDepart());
    }

    @Test
    public void test2(){
        GradeMapper gradeMapper = sqlSession.getMapper(GradeMapper.class);
        Grade grade = gradeMapper.selectGradeById(1);
        System.out.println(grade);
        for (Student student : grade.getStudentList()) {
            System.out.println(student);
        }
    }

    @Test
    public void test3(){
        GradeMapper gradeMapper = sqlSession.getMapper(GradeMapper.class);
        Grade grade = gradeMapper.selectGradeById2(1);
//        System.out.println(grade);
        System.out.println("再此之前 没有使用 班级里的学生信息 ，开启了懒加载");
        for (Student student : grade.getStudentList()) {
            System.out.println(student);
        }
    }



}
